package com.java.test.system.gametypemanage;

import com.java.test.system.gametypemanage.dto.GameTypeAddDTO;
import com.java.test.system.gametypemanage.dto.GameTypeQueryDTO;
import com.java.test.system.gametypemanage.dto.GameTypeUpdateDTO;
import com.java.test.system.gametypemanage.entity.GameTypeEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 游戏类型DAO
 */
@Component
public class GameTypeManageDAO {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 查询
     * @param queryDTO
     * @author
     * @date 2020/7/15 0015 13:58
     * @return List<GameTypeEntity>
     */
    public List<GameTypeEntity> selectGameTypeList(GameTypeQueryDTO queryDTO) {

        StringBuffer sql = new StringBuffer("select * from sys_game_type where 1=1 ");
        Object[] params = new Object[]{};
        if(queryDTO.getStatus()>0){
            sql.append(" and status = ?");
            params = new Object[]{queryDTO.getStatus()};
        }
        sql.append(" order by sort desc");

        List<GameTypeEntity> list = jdbcTemplate.query(sql.toString(), new GameTypeEntity(),params);

        return list;
    }

    /**
     * 获取游戏类型BY游戏名称
     * @param gName
     * @return
     */
    public GameTypeEntity getByGName(String gName) {
        String sql = "select * from sys_game_type where gname =?";
        GameTypeEntity info = null;
        try {
            info= jdbcTemplate.queryForObject(sql, new GameTypeEntity(), gName);
        }catch (EmptyResultDataAccessException e){
            return null;
        }
        return info;
    }

    /**
     * 获取游戏类型BYid
     * @param id
     * @return
     */
    public GameTypeEntity getById(String id) {
        String sql = "select * from sys_game_type where id = ?";
        GameTypeEntity info = null;
        try {
            info = jdbcTemplate.queryForObject(sql, new GameTypeEntity(), id);
        }catch (EmptyResultDataAccessException e){
            return null;
        }

        return info;
    }

    /**
     * 插入游戏类型
     * @param addDTO
     */
    public int insertGameType(GameTypeAddDTO addDTO) {
        String sql ="insert into sys_game_type(gname,remarks,operation,createTime,status) " +
                "values (?,?,?,?,?)";
        return jdbcTemplate.update(sql,addDTO.getName(),addDTO.getRemarks(),addDTO.getOperation(),addDTO.getCreateTime(),addDTO.getStatus());
    }

    /**
     * 判断游戏类型是否存在去掉id
     * @param name
     * @param id
     * @return
     */
    public GameTypeEntity getByKeyExcludeId(String name, String id) {
        String sql = "select * from sys_game_type where gname = ? and id <> ?";
        GameTypeEntity info = null;
        try {
            info = jdbcTemplate.queryForObject(sql, new GameTypeEntity(),name,id);
        }catch (EmptyResultDataAccessException e){
            return null;
        }

        return info;
    }

    /**
     * 修改游戏类型
     * @param entity
     */
    public int updateById(GameTypeUpdateDTO entity) {
        String sql = "update sys_game_type " +
                "set  gname = ?,remarks = ?,modify = ?,modifyTime = ? , sort = ?, status=?  " +
                "where id = ?";

        return jdbcTemplate.update(sql, entity.getName(), entity.getRemarks(), entity.getModify(),entity.getModifyTime(),entity.getSort(),entity.getStatus(),entity.getId());
    }

    /**
     * 删除游戏类型BYId
     * @param id
     */
    public int deleteById(String id) {
        String sql ="DELETE FROM sys_game_type WHERE id = ?";
        return jdbcTemplate.update(sql,id);
    }
}
